from django.db import models
from django.core.validators import RegexValidator

phone_regex = RegexValidator(
    regex=r'^1[3-9]\d{9}$',
    message="手机号格式不正确"
)

# Create your models here.
class BaseActive(models.Model):
    active = models.SmallIntegerField(verbose_name="状态",default=1,choices=((1,"激活"),(0,'删除'),))
    class Meta:
        abstract = True
        """
        1. 抽象基类
        它将定义的模型类（如 BaseActive ）标记为抽象基类。抽象基类不会对应数据库中的实际表 。它主要用于抽取一些通用的字段和方法，供其他模型类继承使用。
        """

class Administrator(BaseActive):
    username = models.CharField(verbose_name='用户名',max_length=20,db_index=True)
    password = models.CharField(verbose_name="密码",max_length=64)
    mobile = models.CharField(verbose_name="电话号",max_length=11,db_index=True,validators=[phone_regex])
    # 手机号验证
    create_date = models.DateTimeField(verbose_name='创建日期',auto_now_add=True)

class Level(BaseActive):
    title = models.CharField(verbose_name="标题",max_length=20,db_index=True)
    percent = models.IntegerField(verbose_name="折扣",help_text="请输入0-100的整数")
    def __str__(self):
        return self.title

class Customer(BaseActive):
    username = models.CharField(verbose_name='用户名',max_length=20,db_index=True)
    password = models.CharField(verbose_name='密码',max_length=64)
    mobile = models.CharField(verbose_name="电话号",max_length=11,db_index=True,validators=[phone_regex])
    level = models.ForeignKey(verbose_name='级别',to=Level,on_delete=models.CASCADE)
    balance = models.DecimalField(verbose_name="账户余额",max_digits=10,default=0,decimal_places=2)
    create_date = models.DateTimeField(verbose_name="创建日期",auto_now_add=True)
    creator = models.ForeignKey(verbose_name='创建者',to=Administrator,on_delete=models.CASCADE)

class PricePolicy(models.Model):
    price = models.DecimalField(verbose_name='价格',max_digits=10,decimal_places=2,default=0)
    count = models.IntegerField(verbose_name="数量")

class Order(BaseActive):
    status_choices = (
        (1,"待执行"),
        (2,"正在执行"),
        (3,"已完成"),
        (4,"执行失败"),
        (5,"撤单")
    )
    status = models.SmallIntegerField(verbose_name="订单状态",choices=status_choices,default=1,db_index=True)
    oid = models.CharField(verbose_name="订单号",max_length=64,unique=True)
    url = models.CharField(verbose_name="订单网址",max_length=64)
    count = models.IntegerField(verbose_name="订单数量")
    price = models.DecimalField(verbose_name="价格",max_digits=10,decimal_places=2,default=0)
    real_price = models.DecimalField(verbose_name="真实价格",max_digits=10,decimal_places=2,default=0)
    old_view_count = models.CharField(verbose_name="原播放量",max_length=20)
    create_date = models.DateTimeField(verbose_name='创建日期',auto_now_add=True)
    customer = models.ForeignKey(verbose_name="客户",to='Customer',on_delete=models.CASCADE)
    meom = models.CharField(verbose_name="备注",null=True,blank=True,max_length=64)

class TransactionRecord(BaseActive):
    """ 交易记录 """
    charge_type_class_mapping = {
        1: "success",
        2: "danger",
        3: "default",
        4: "info",
        5: "primary",
    }
    charge_type_choices = ((1, "充值"), (2, "扣款"), (3, "创建订单"), (4, "删除订单"), (5, "撤单"),)
    charge_type = models.SmallIntegerField(verbose_name="类型", choices=charge_type_choices)

    customer = models.ForeignKey(verbose_name="客户", to="Customer", on_delete=models.CASCADE)
    amount = models.DecimalField(verbose_name="金额", default=0, max_digits=10, decimal_places=2)

    creator = models.ForeignKey(verbose_name="管理员", to="Administrator", on_delete=models.CASCADE, null=True, blank=True)

    order_oid = models.CharField(verbose_name="订单号", max_length=64, null=True, blank=True, db_index=True)
    create_date = models.DateTimeField(verbose_name="交易时间", auto_now_add=True)
    memo = models.TextField(verbose_name="备注", null=True, blank=True)
